<template>
  <div class="search-box">
    <img src="~@/assets/images/manhua/sousuo@2x.png">
    <input ref="query" v-model="query" @change="SearchCardname" @keyup.enter="SearchCardname" class="box" :placeholder="placeholder"/>
    <i @click.stop="clear" v-show="query" class="clearbtn">
      <!-- <img src="~@/assets/images/home/nav_fork.png"> -->
    </i>
  </div>
</template>

<script type="text/ecmascript-6">
  import { debounceSim } from '@/utils'

  export default {
    name: 'search-box',
    props: {
      placeholder: {
        type: String,
        default: '请输入搜索关键词'
      }
    },
    data() {
      return {
        query: ''
      }
    },
    methods: {
      clear() {
        this.query = ''
      },
      focus() {
        this.$refs.query.focus()
      },
      setQuery(query) {
        this.query = query
      },
      SearchCardname() {
        setTimeout(() => {
          if (this.query) {
            console.log('this.query', this.query)
            this.$emit('search', this.query)
          }
        }, 10)
      },
      blur() {
        this.$refs.query.blur()
      }
    },
    created() {
      this.$watch('query', debounceSim((newQuery) => {
        this.$emit('query', newQuery)
      }, 200))
    }
  }
</script>

<style rel="stylesheet/scss" lang="scss" scoped>
@import "src/assets/css/mixin.scss";
@import 'src/assets/css/variables.scss';

  .search-box{
    display: flex;
    align-items: center;
    box-sizing: border-box;
    width: 100%;
    padding: 0 10px;
    height: 30px;
    background: $color-inputBg;
    border-radius: 15px;
    input{
      height: 30px;
    }
    img{
      width: 13px;
    }
    .box{
      flex: 1;
      margin: 0 5px;
      outline: none;
      line-height: 18px;
      background: $color-inputBg;
      color: $color-input;
      font-size: $font-size-medium;
      &::placeholder{
        color: $color-input;
      }
    }
    .clearbtn{
      font-size: 16px;
    }
  }
</style>
